import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/report/readHistoryResult.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/readingLatestDataGrid.dart';
import '../../themes/pageStyles.dart';

class LatestReadingRecords extends StatefulWidget {
  static const String routeName = '/student/report/readingrecords';

  @override
  State<LatestReadingRecords> createState() => _LatestReadingRecordsState();
}

class _LatestReadingRecordsState extends State<LatestReadingRecords> {
  final box = Hive.box('appconfig');
  final ReportsController _reportsController = Get.find();
  bool isStudentOrIndividual = false;
  List<HistoryResult> historyResulsts = [];

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    isStudentOrIndividual = box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Individual ||
        box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Student;

    if (isStudentOrIndividual) {
      _reportsController.fetchMyReadingRecords().then((result) {
        setState(() {
          historyResulsts = result;
        });
      });
    }
    super.initState();
  }

  final PageStyles _pageStyles = PageStyles();

  @override
  Widget build(BuildContext context) {
    Log.debug('build student MyReadingRecords');

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: _pageStyles.dataGridBackground(),
        child: Container(
          margin: EdgeInsets.all(12.0),
          decoration: _pageStyles.dataGridForground(),
          child: LatestReadingHistoryDataGrids(historyResulsts),
        ),
      ),
    );
  }
}
